package com.art.sunflower.service.impl;

import com.art.sunflower.enumeration.CourseStatusType;
import com.art.sunflower.exception.NullCourseException;
import com.art.sunflower.mapper.CourseMapper;
import com.art.sunflower.pojo.Course;
import com.art.sunflower.pojo.Lay;
import com.art.sunflower.pojo.User;
import com.art.sunflower.service.CourseService;
import com.art.sunflower.util.MyPageInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class CourseServiceImpl implements CourseService {
    @Autowired
    @Resource
    private CourseMapper courseMapper;

    @Override
    public int addCourse(Course course) {
        return courseMapper.addCourse(course);
    }


    @Override
    public int deleteById(int id) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        return courseMapper.deleteById(id);
    }


    @Override
    public int updateCourseById(int id, String courseName, String introduce, String requirement, int teachFee, int materialFee, int cooperationId) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        return courseMapper.updateCourseById(id, courseName, introduce, requirement, teachFee, materialFee, cooperationId);
    }

    @Override
    public Lay findAllCourse(Integer pagenum, Integer pagesize) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        //需要进行分页
        PageHelper.startPage(pagenum, pagesize);
        List<Map<String, Object>> courseList = (List<Map<String, Object>>) courseMapper.findAllCourse(user.getUsername());
        PageInfo info = new PageInfo(courseList);//创建pageinfo，包含分页的信息
        Lay lay2 = new Lay();
        lay2.setPagenum(pagenum);
        lay2.setCount(info.getTotal());//总条数
        lay2.setData(info.getList());//显示的数据
        return lay2;
    }

    @Override
    public Map<String, Object> selectOneCourse(int id) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        return courseMapper.selectOneCourse(id);
    }

    @Override
    public int updateCourseStatu(int id, CourseStatusType statu) {
        return courseMapper.updateCourseStatu(id, statu);
    }

    @Override
    public Lay findAllCourseByAdmin(Integer pagenum, Integer pagesize) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        //需要进行分页
        PageHelper.startPage(pagenum, pagesize);
        List<Map<String, Object>> courseList = (List<Map<String, Object>>) courseMapper.findAllCourseByAdmin(pagenum, pagesize);
        PageInfo info = new PageInfo(courseList);//创建pageinfo，包含分页的信息
        Lay lay2 = new Lay();
        lay2.setPagenum(pagenum);
        lay2.setCount(info.getTotal());//总条数
        lay2.setData(info.getList());//显示的数据
        return lay2;
    }

    @Override
    public MyPageInfo findAllCourseByAdmin2(Integer pagenum, Integer pagesize) {
        //需要进行分页
        PageHelper.startPage(pagenum, pagesize);
        List<Map<String, Object>> courseList = courseMapper.findAllCourseByAdmin2();
        for (Map<String, Object> stringObjectMap : courseList) {
            if (stringObjectMap.get("status").toString().equals("审定通过")) {
                stringObjectMap.put("status", true);
            } else if (stringObjectMap.get("status").toString().equals("审定失败")) {
                stringObjectMap.put("status", false);
            }
        }
        PageInfo<Map<String, Object>> info = new PageInfo<>(courseList);//创建pageinfo，包含分页的信息
        return new MyPageInfo(info);
    }


    public int courseStatuCheck(int id, int teachFee, int materialFee, CourseStatusType statu) {
        return courseMapper.courseStatuCheck(id, teachFee, materialFee, statu);
    }

    public int selectMst(int id) {
        return courseMapper.selectMst(id);
    }

    @Override
    public List<Map<String, Object>> getNameList() throws NullCourseException {
        List<Map<String, Object>> course = courseMapper.selectName();
        if (course.size() == 0)
            throw new NullCourseException();
        return course;
    }

    public Map<String, Object> findOneCourseByAdmin(int id) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        return courseMapper.findOneCourseByAdmin(id);
    }


}
